﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApi.Adapters.Interfaces;
using WebApi.Data;
using WebApi.Model;
using WebApi.Models;

namespace WebApi.Adapters.Adapters
{
    public class MovieAdapter : IMovieAdapter
    {
        public List<MovieVM> GetAllMovies()
        {
            List<MovieVM> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Movies.Select(x => new MovieVM()
                {
                    Director = x.Director,
                    MovieId = x.MovieId,
                    Rating = x.Rating.ToString(),
                    Title = x.Title
                }).ToList();
            }
            return model;
        }

        public int AddMovie(Movie model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Movies.Add(model);
                db.SaveChanges();
            }
            return model.MovieId;
        }

        public int DeleteMovie(int id)
        {
            int result;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Movies.Remove(db.Movies.FirstOrDefault(x => x.MovieId == id));
                result = db.SaveChanges();
            }
            return result;
        }
    }
}